package cn.ccit.DAOImp;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.ccit.util.MyDBUtil;
import cn.ccit.DAO.CancelDishRecordDAO;
import cn.ccit.entites.record.CancelDishRecord;

public class CancelDishRecordDAOimp implements CancelDishRecordDAO{
	private Session session;
	private static final Log log = LogFactory.getLog(CancelDishRecordDAOimp.class);
	@Override
	public boolean addCancelDishRecord(CancelDishRecord cdRecord) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.save(cdRecord);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("add CancelDishRecord fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}
	@Override
	public boolean removeCancelDishRecord(CancelDishRecord cdRecord) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.delete(cdRecord);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("remove CancelDishRecord fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}
	@Override
	public boolean changeCancelDishRecordInfo(CancelDishRecord cdRecord) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				session.update(cdRecord);
				session.beginTransaction().commit();
				return true;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("change CancelDishRecord fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return false;
	}
	@Override
	public List<CancelDishRecord> getSomeCancelDishRecords(int start, int limit) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from CancelDishRecord as cancelDishRecord order by id desc";
				Query query=session.createQuery(hql);
				Long count=(Long)session.createQuery("select count(*) from CancelDishRecord").uniqueResult();
				if(count>limit){
					query.setFirstResult(start);
					query.setMaxResults(limit);
				}
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("getSomeCancelDishRecord fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}
	@Override
	public CancelDishRecord searchCancelDishRecordByID(int id) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from CancelDishRecord as cancelDishRecord where cancelDishRecord.id=? ";
				Query query=session.createQuery(hql);
				query.setInteger(0, id);
				CancelDishRecord cancelDishRecord=(CancelDishRecord)query.list().get(0);
				session.beginTransaction().commit();
				return cancelDishRecord; 
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search CancelDishRecord fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}
	@Override
	public List<CancelDishRecord> searchCancelDishRecordByYear(String year) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from CancelDishRecord as cancelDishRecord where cancelDishRecord.orderYear=?";
				Query query=session.createQuery(hql);
				query.setString(0, year);
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search CancelDishRecord by year fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}
	@Override
	public List<CancelDishRecord> searchCancelDishRecordByMonth(String month) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from CancelDishRecord as cancelDishRecord where cancelDishRecord.orderMonth=?";
				Query query=session.createQuery(hql);
				query.setString(0, month);
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search CancelDishRecord by month fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}
	@Override
	public List<CancelDishRecord> searchCancelDishRecordByDate(String date) {
		session=MyDBUtil.getSession();
		if(session!=null){
			try {
				session.beginTransaction();
				String hql="from CancelDishRecord as cancelDishRecord where cancelDishRecord.orderDate=?";
				Query query=session.createQuery(hql);
				query.setString(0, date);
				List list=query.list();
				session.beginTransaction().commit();
				return list;
			} catch (Exception e) {
				e.printStackTrace();
				log.error("search CancelDishRecord by date fail", e);
				MyDBUtil.rollBack();
			}finally{
				session.close();
			}
		}
		return null;
	}
	
}
